package com.shadow.bus.main.work;

import android.app.Activity;
import android.app.Dialog;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shadow.bus.entity.TroubleEntity;
import com.shadow.bus.entity.TroubleOperationEntity;
import com.tepth.shadow.ui.recycler.RecyclerViewNoBugLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import busfix.tepth.com.shadow_bus.R;

/**
 * @author zhangyu
 * @date 2017/7/13
 */

public class AddOtherTroubleDialog extends Dialog {
    private List<TroubleEntity> mSelectList = new ArrayList<>();

    public AddOtherTroubleDialog(@NonNull final Activity context,
                                 final TroubleOperationEntity troubleBean,
                                 final OnAddOtherTroubleListener listener,
                                 final List<TroubleEntity> list) {
        super(context);

        setTitle("本次作业消除的其它故障");

        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_add_other_trouble, null);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new RecyclerViewNoBugLinearLayoutManager(getContext()));
        recyclerView.setAdapter(new TroubleAdapter(R.layout.item_add_other_trouble, list));
        view.findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null && !mSelectList.isEmpty()) {
                    for (TroubleEntity remarkTrouble : mSelectList) {
                        ArrayList<TroubleOperationEntity> troubleBeans = remarkTrouble.getShareOperation();
                        if (troubleBeans == null) {
                            troubleBeans = new ArrayList<>();
                            remarkTrouble.setShareOperation(troubleBeans);
                        }
                        troubleBeans.add(troubleBean);
                        remarkTrouble.setChecked(false);
                    }
                    listener.onAdd(mSelectList);
                }
                dismiss();
            }
        });
        setContentView(view);
    }


    interface OnAddOtherTroubleListener {
        void onAdd(List<TroubleEntity> remarkTroubles);
    }

    public class TroubleAdapter extends BaseQuickAdapter<TroubleEntity, BaseViewHolder> {

        public TroubleAdapter(int layoutResId, @Nullable List<TroubleEntity> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final TroubleEntity item) {
            CheckBox checkBox = helper.getView(R.id.checkbox);
            checkBox.setChecked(item.isChecked());
            checkBox.setText(item.getName().replaceAll("自定义录入@@", "附加作业项"));
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mSelectList != null) {
                        if (item.isChecked()) {
                            item.setChecked(false);
                            mSelectList.remove(item);
                        } else {
                            item.setChecked(true);
                            mSelectList.add(item);
                        }
                    }
                }
            });
        }
    }

}
